package com.photographysharing.filter;

import com.photographysharing.common.Information;
import com.photographysharing.common.ThreadLocalOperation;
import com.photographysharing.util.JWTUtil;
import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Component;

import java.io.IOException;
@Component
public class requestFfilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String userId = JWTUtil.getUserIdFromToken(request);
        String role = JWTUtil.getRoleFromToken(request);
        String email = JWTUtil.getEmailFromToken(request);
        ThreadLocalOperation threadLocalOperation = new ThreadLocalOperation();
        Information information = new Information();
        if(userId != null){
            information.setUserId(userId);
            information.setRole(role);
            information.setEmail(email);
        }
        threadLocalOperation.setThreadLocal(information);
        filterChain.doFilter(servletRequest,servletResponse);
    }
}
